package com.tinyengine.it.common.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * Security工具类
 */
public class SecurityUtils {

    /**
     * 获取当前登录用户名
     * @return 当前登录用户名,如果未登录返回null
     */
    public static String getCurrentUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
            return ((UserDetails) authentication.getPrincipal()).getUsername();
        }
        return "1";
    }

    /**
     * 获取当前登录用户ID
     * @return 当前登录用户ID,如果未登录返回"1"
     */
    public static String getCurrentUserId() {
        String username = getCurrentUsername();
        if (username != null) {
            return username; // 暂时使用用户名作为ID,后续可以通过查询数据库获取真实ID
        }
        return "1"; // 默认返回1
    }
} 